let logger = require("pomelo-logger").getLogger(__filename);
let swiperItemCreator = require("../entities/swiperItem").create;
let swiperItemLoader = require("../entities/swiperItem").loadFromDocument;
let DateUtil = require("../../utils/dateUtil");

let SwiperItemService = function (app) {
    this.app = app;
    this.swiperItems = [];
    this.nextSwiperItemId = 0;
};

SwiperItemService.prototype.initialize = function () {
    let swiperItemDao = this.app.get("swiperItemDao");
    let maxSwiperItemId = 0;
    //load from mongo
    swiperItemDao.findAllSwiperItems(documents => {
        for(let document of documents) {
            let swiperItem = swiperItemLoader(document);
            this.swiperItems.push(swiperItem);
            if(swiperItem.id >= maxSwiperItemId) {
                maxSwiperItemId = swiperItem.id;
            }
        }
        this.nextSwiperItemId = maxSwiperItemId + 1;
        this.sort();
    });
};

SwiperItemService.prototype.getSwiperItems = function () {
    return this.swiperItems;
};

SwiperItemService.prototype.getSwiperItemById = function (id) {
    for(let swiperItem of this.swiperItems) {
        if(id === swiperItem.id) {
            return swiperItem;
        }
    }
    return null;
};

SwiperItemService.prototype.updateSwiperItemUrl = function (id, url, callback) {
    let swiperItemDao = this.app.get("swiperItemDao");
    if(!this.contains(id)) {
        callback(null);
        return;
    }
    for(let swiperItem of this.swiperItems) {
        if(id === swiperItem.id) {
            swiperItemDao.updateUrl(id, url, result => {
                if(!result || result.result.ok != 1) {
                    logger.info(`updateSwiperItemUrl error. document null`);
                    callback(null);
                    return;
                }
                swiperItem.url = url;
                callback(swiperItem);
            });
        }
    }
};

SwiperItemService.prototype.updateSwiperItemPriority = function (id, priority, callback) {
    let swiperItemDao = this.app.get("swiperItemDao");
    if(!this.contains(id)) {
        callback(null);
        return;
    }
    for(let swiperItem of this.swiperItems) {
        if(id === swiperItem.id) {
            swiperItemDao.updatePriority(id, priority, result => {
                if(!result || result.result.ok != 1) {
                    logger.info(`updateSwiperItemPriority error. document null`);
                    callback(null);
                    return;
                }
                swiperItem.priority = priority;
                this.sort();
                callback(swiperItem);
            });
        }
    }
};

SwiperItemService.prototype.updateSwiperItem = function (id, priority, url, activityId, startTime, endTime, callback) {
    let swiperItemDao = this.app.get("swiperItemDao");
    if(!this.contains(id)) {
        callback(null);
        return;
    }
    for(let swiperItem of this.swiperItems) {
        if(id === swiperItem.id) {
            swiperItemDao.update(id, priority, url, activityId, startTime, endTime, result => {
                if(!result || result.result.ok != 1) {
                    logger.info(`updateSwiperItem error. document null`);
                    callback(null);
                    return;
                }
                swiperItem.priority = priority;
                swiperItem.url = url;
                swiperItem.activityId = activityId;
                swiperItem.startTime = startTime;
                swiperItem.endTime = endTime;
                this.sort();
                callback(swiperItem);
            });
        }
    }
};

SwiperItemService.prototype.addSwiperItem = function (priority, url, activityId, startTime, endTime, callback) {
    let swiperItemDao = this.app.get("swiperItemDao");
    let swiperItem = swiperItemCreator(this.nextSwiperItemId, priority, url, activityId, startTime, endTime);
    swiperItemDao.insertOne(swiperItem.toBSON(), result => {
        if(!result || result.result.ok != 1) {
            logger.info(`addSwiperItem error. id empty`);
            callback(null);
            return;
        }
        this.swiperItems.push(swiperItem);
        this.sort();
        this.nextSwiperItemId ++;
        callback(swiperItem);
    });
};

SwiperItemService.prototype.deleteSwiperItemById = function (id, callback) {
    let swiperItemDao = this.app.get("swiperItemDao");
    swiperItemDao.deleteById(id, result => {
        if(!result || result.ok != 1) {
            logger.info(`addSwiperItem error. id empty`);
            callback(null);
            return;
        }
        let swiperItems = [];
        this.swiperItems.forEach(swiperItem => {
            if(swiperItem.id !== id) {
                swiperItems.push(swiperItem);
            }
        });
        this.swiperItems = swiperItems;
        this.sort();
        callback(id);
    });
};

SwiperItemService.prototype.getEffectSwiperJSON = function () {
    let serializeSwiperItems = [];
    let now = DateUtil.nowMS();
    this.swiperItems.forEach(swiperItem => {
        if(now > swiperItem.startTime && now < swiperItem.endTime) {
            serializeSwiperItems.push(swiperItem.toJSON());
        }
    });
    return serializeSwiperItems;
};

SwiperItemService.prototype.toJSON = function () {
    let serializeSwiperItems = [];
    this.swiperItems.forEach(swiperItem => {
        serializeSwiperItems.push(swiperItem.toJSON());
    });
    return serializeSwiperItems;
};

SwiperItemService.prototype.contains = function (id) {
   for(let swiperItem of this.swiperItems) {
       if(swiperItem.id === id) {
            return true;
       }
   }
   return false;
};

SwiperItemService.prototype.sort = function () {
    this.swiperItems.sort(function (s1, s2) {
        if(s1.priority < s2.priority) {
            return -1;
        }else if(s1.priority > s2.priority) {
            return 1;
        }else{
            return s1.id < s2.id ? 1 : -1;
        }
    });
};

module.exports.create = function (app) {
    return new SwiperItemService(app);
};
